<template>
  <div class="success-container">
    <!-- SVG 插图 -->
    <div class="illustration">
      <svg width="150" height="150" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z"
              fill="#73D478FF" />
      </svg>
    </div>

    <!-- 标题与信息 -->
    <h2 class="title">🎉 支付成功！</h2>
    <p class="desc">感谢您选择履程</p>

    <!-- 倒计时 -->
    <p v-if="countdown > 0" class="countdown-text">页面将在 {{ countdown }} 秒后自动跳转至首页</p>
    <p v-else class="countdown-text">正在跳转...</p>

    <!-- 订单信息 -->
    <div class="order-info">
      <p><strong>订单号：</strong>{{ route.query.number }}</p>
      <p><strong>支付金额：</strong><span class="amount">{{ route.query.amount }} 元</span></p>
    </div>

    <!-- 操作按钮 -->
    <div class="actions">
      <button @click="cancelRedirect();$router.push('/order/list')">查看订单</button>
      <button @click="cancelRedirect();$router.push('/')">返回首页</button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()

// 倒计时变量 5秒后跳转
const countdown = ref(5)
let redirectTimer = null // 保存倒计时引用

// 取消跳转函数
const cancelRedirect = () => {
  if (redirectTimer) {
    clearInterval(redirectTimer)
    redirectTimer = null
  }
}

// 自动跳转逻辑
onMounted(() => {
  redirectTimer = setInterval(() => {
    if (countdown.value <= 1) {
      clearInterval(redirectTimer)
      redirectTimer = null
      router.push('/')
    } else {
      countdown.value--
    }
  }, 1000)
})

</script>

<style scoped lang="scss">
$primary-color: #ec9948;

.success-container {
  max-width: 600px;
  margin: 40px auto;
  padding: 40px 30px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.08);
  text-align: center;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  animation: fadeInUp 0.6s ease-out;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.illustration {
  margin-bottom: 20px;
}

.title {
  font-size: 28px;
  color: #333;
  margin-bottom: 10px;
}

.desc {
  font-size: 16px;
  color: #666;
  margin-bottom: 20px;
}

.countdown-text {
  font-size: 14px;
  color: #888;
  margin-top: -10px;
  margin-bottom: 20px;
}

.order-info {
  background: #f9f9f9;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 30px;
  text-align: left;
  display: inline-block;
  min-width: 80%;

  p {
    margin: 10px 0;
    color: #555;
  }

  .amount {
    color: $primary-color;
    font-size: 18px;
    font-weight: bold;
  }
}

.actions {
  display: flex;
  gap: 20px;
  justify-content: center;
}

.actions button {
  flex: 1;
  padding: 12px 20px;
  font-size: 16px;
  font-weight: 500;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  background: #f0f0f0;
  color: #333;
}

.actions button:hover {
  background: #eaeaea;
}

.actions button:first-child {
  background: $primary-color;
  color: white;
}

.actions button:first-child:hover {
  background: darken($primary-color, 6%);
}
</style>
